package com.example.demo.controller;
import com.example.demo.entity.Msg;
import com.example.demo.entity.ReturnT;
import com.example.demo.repository.MsgRepository;
//import com.software.system.entity.Msg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @description msg
 * @author mqp
 * @date 2022-05-10
 */

@Slf4j
@Api(tags = "msg")
@CrossOrigin
@RestController
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    private MsgRepository msgRepository;

    /**
    * 新增或编辑
    */
    @PostMapping("/save")
    @ApiOperation(value = "save Msg", notes = "save Msg")
    public Object save(@RequestBody Msg msg){
        try {
            return ReturnT.success(msgRepository.save(msg));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.error("保存失败");
        }

    }

    /**
    * 删除
    */
    @PostMapping("/delete")
    @ApiOperation(value = "delete Msg", notes = "delete Msg")
    public Object delete(int id){
        Optional<Msg> msg=msgRepository.findById(id);
        if(msg.isPresent()){
            msgRepository.deleteById(id);
            return ReturnT.success("删除成功");
        }else{
            return ReturnT.error("没有找到该对象");
        }
    }

    /**
    * 查询
    */
    @PostMapping("/find")
    @ApiOperation(value = "find Msg by id", notes = "find Msg by id")
    public Object find(int id){
        Optional<Msg> msg=msgRepository.findById(id);
        if(msg.isPresent()){
            return ReturnT.success(msg.get());
        }else{
            return ReturnT.error("没有找到该对象");
        }
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "list Msg", notes = "list Msg")
    public Object getAll() {

        try {
            return ReturnT.success(msgRepository.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.error(e.getMessage());
        }

    }

    /**
    * 分页查询
    */
//    @PostMapping("/list")
    @GetMapping("/list")
    @ApiOperation(value = "list Msg", notes = "list Msg")
    public Object list(@RequestBody(required = false) Msg msg,
                        @RequestParam(required = false, defaultValue = "0") int pageNumber,
                        @RequestParam(required = false, defaultValue = "10") int pageSize) {

            try {
                //创建匹配器，需要查询条件请修改此处代码
                ExampleMatcher matcher = ExampleMatcher.matchingAll();

                //创建实例
                Example<Msg> example = Example.of(msg, matcher);
                //分页构造
                Pageable pageable = PageRequest.of(pageNumber,pageSize);

                return ReturnT.success(msgRepository.findAll(example, pageable));

            } catch (Exception e) {
                e.printStackTrace();
                return ReturnT.error(e.getMessage());
            }

    }

}
